package com.medicine.hbp.ui;

import java.util.regex.Matcher;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.uitl.PatternUtils;
import com.medicine.hbp.uitl.ToastUtil;

public class PasswordBackActivity extends BaseActivity{
	@BindView(id = R.id.text_title, click = false)
	public TextView text_title;
	@BindView(id = R.id.btn_image_left_exit, click = true)
	public ImageView btn_image_left_exit;
	/** 手机号 */
	@BindView(id = R.id.edit_name_r)
	public EditText edit_phone_r;
	@BindView(id = R.id.edit_password_yes)
	public EditText edit_password;
	@BindView(id = R.id.edit_yanzheng_code)
	public EditText edit_yanzheng_code;
	/** 获取验证码 */
	@BindView(id = R.id.btn_smscode, click = true)
	public Button btn_smscode;
	@BindView(id = R.id.btn_password, click = true)
	public Button btn_password;

	private TimeCount time;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_passwordback);
	}
	@Override
	public void initWidget() {
		super.initWidget();
		time = new TimeCount(60000, 1000);

		text_title.setText("密码找回");
	}
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_smscode:
				httpSmsCode();
				break;
			case R.id.btn_password:
				httpBackPass();
				break;
			case R.id.btn_image_left_exit:
				finish();
				break;
			default:
				break;
		}
	}
	/**
	 * 短信发送
	 */
	private void httpSmsCode() {
		String phone=edit_phone_r.getText().toString();
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showShort(aty, "请输入手机号");
			return;
		}
		Matcher matcherMailbox = PatternUtils.patternNumber
				.matcher(phone);
		if (!matcherMailbox.matches()) {
			ToastUtil.showShort(aty, "请输入正确的手机号");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("phone", phone);// 用户账户
		params.put("smsType", "1");// 1找回密码短信 0注册短信

		AppHttps.HttpPost(this,HttpInterface.SMS_CODE, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						ToastUtil.showShort(aty, "短信已发送,请注意查收");
						time.start();// 开始计时
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 找回密码接口
	 */
	private void httpBackPass() {
		String phone=edit_phone_r.getText().toString();
		String code=edit_yanzheng_code.getText().toString();
		if(TextUtils.isEmpty(code)){
			ToastUtil.showShort(aty, "请输入手机验证码");
			return;
		}
		String psd=btn_password.getText().toString();
		if(TextUtils.isEmpty(psd)){
			ToastUtil.showShort(aty, "请输入新密码");
			return;
		}
		HttpParams params = new HttpParams();
		params.put("account", phone);// 用户账户(手机号(发送短信号码))
		params.put("password", psd);// 用户密码
		params.put("code", code);// 短信码
		AppHttps.HttpPost(this,HttpInterface.BACK_PASS, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						finish();
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {// 计时完毕
			btn_smscode.setText("获取验证码");
			btn_smscode.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程
			btn_smscode.setClickable(false);// 防止重复点击
			btn_smscode.setText(millisUntilFinished / 1000 + "s");
		}
	}
}
